﻿#region Header

/***************************************************************************
Author : Krishan Kaliraman (www.sharedwith.me)  Date: 1 Aug 2012
***************************************************************************/

#endregion Header
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Common
{
    public class DiscussionActivity
    {
        private Activity activity;
        public string DiscussionActivityType { get; set; }
        public string DiscussionActivityId { get; set; }
        public WorkItem WorkItem { get; set; }
        public string DescriptionHtml { get; set; }
        public string AcceptanceCriteria { get; set; }
        public List<DiscussionItem> DiscussionHistory { get; set; }
        public List<ActivityAttachment> Attachments = new List<ActivityAttachment>();
        public DiscussionItem DiscussionEntered { get; set; }

        public DiscussionActivity()
        {

        }

        public DiscussionActivity(WorkItem workItem)
        {
            activity = new Activity(workItem);
            activity.FillDiscussionHistoryOfActivity();
            activity.FillDiscussionPageRequiredProperties();
            FillRequiredProperties();
        }
        

        public DiscussionActivity UploadAttachmentToDiscussionActivity(string filePath, string comment)
        {
            activity = new Activity();
            activity.WorkItem = WorkItem;
            activity.UploadAttachment(filePath, comment);
            DiscussionActivity newDiscussionActivity = new DiscussionActivity(WorkItem);
            return newDiscussionActivity;
        }

        public DiscussionActivity UploadAttachmentAndDiscussionItemToDiscussionActivity(string filePath, string comment , DiscussionItem discussionItem)
        {
            activity = new Activity();
            activity.WorkItem = WorkItem;
            string discussionEntryId;
            activity.UploadAttachmentAndEnterDiscussionItem(filePath,comment,discussionItem,out discussionEntryId);
            DiscussionActivity newDiscussionActivity = new DiscussionActivity(WorkItem);
            foreach (DiscussionItem discussion in newDiscussionActivity.DiscussionHistory.Where(discussion => discussion.DiscussionEntryId == discussionEntryId))
            {
                newDiscussionActivity.DiscussionEntered = discussion;
                break;
            }
            return newDiscussionActivity;
        }

        private void FillRequiredProperties()
        {
            DescriptionHtml = activity.DescriptionHtml;
            AcceptanceCriteria = activity.AcceptanceCriteria;
            DiscussionHistory = activity.DiscussionHistory;
            Attachments = activity.Attachments;
            DiscussionActivityType = activity.WorkItemType;
            DiscussionActivityId = activity.Id;
            foreach(DiscussionItem discussionItem in DiscussionHistory)
            {
                if (discussionItem.DocumentsAttached != null)
                {
                    foreach (ActivityAttachment documentAttached in discussionItem.DocumentsAttached)
                    {
                        foreach (ActivityAttachment activityAttachment in Attachments)
                        {
                            if (documentAttached.Name == activityAttachment.Name)
                            {
                                documentAttached.Url = activityAttachment.Url;
                            }
                        }
                    }
                }
            }
        }

        internal DiscussionActivity SubmitDiscussionItem(DiscussionItem discussionItem)
        {
            activity = new Activity(WorkItem);
            activity.EnterDiscussionHistoryForTheActivity(discussionItem);
            DiscussionActivity newDiscussionActivity = new DiscussionActivity(WorkItem);
            return newDiscussionActivity;
        }
    }
}
